package com.ssh.manager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Company;
import com.ssh.manager.CompanyManager;

@Service
public class CompanyManagerImpl implements CompanyManager {
	@Resource
	private IDao daoImpl;
	
	@Override
	public List<Company> getCompaniesAll() {
		String sql = "from Company where del!=1 order by createDate desc";
		List<Company> companies = (List<Company>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, 0, -1);
		return companies;
	}

	@Override
	public Company addCompany(Company company) {
		return daoImpl.saveDomain(company);
	}

	@Override
	public void findCompaniesByPage(Page page, String isDesc) {
		String sql = "select count(*) from Company where del!=1";
		try {
			int companySum = daoImpl.countAll(sql, null);
			page.setTotalCount(new Long(companySum));
			page.countTotalPageNum();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		sql = "from Company where del!=1 order by createDate "+isDesc;
		List<Company> companies = (List<Company>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(companies);
	}

	@Override
	public boolean deleteCompany(Long companyID) {
		try {
			Company company = daoImpl.getDomain(Company.class, companyID);
			company.setDel(1);
			daoImpl.saveOrUpdateDomain(company);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Company updateCompany(Company company) {
		Company company2 = daoImpl.getDomain(Company.class, company.getId());
		company2.setName(company.getName());
		company2.setContent(company.getContent());
		daoImpl.saveOrUpdateDomain(company2);
		return company2;
	}

	@Override
	public Company getCompanyById(Long companyId) {
		return daoImpl.getDomain(Company.class, companyId);
	}
}
